<?php

/**
 * This is the model class for table "place_locations".
 *
 * The followings are the available columns in table 'place_locations':
 * @property string $id
 * @property string $place_id
 * @property string $address
 * @property string $ward
 * @property string $district
 * @property integer $district_id
 * @property string $city
 * @property integer $city_id
 * @property string $country
 * @property integer $country_id
 * @property string $map
 * @property string $phone
 * @property string $altphone
 * @property string $fax
 */
class PlaceLocations extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'place_locations';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('place_id, address, ward, phone', 'required'),
			array('district_id, city_id, country_id', 'numerical', 'integerOnly'=>true),
			array('place_id', 'length', 'max'=>8),
			array('address, ward, district, city, country, map', 'length', 'max'=>255),
			array('phone, altphone, fax', 'length', 'max'=>20),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, place_id, address, ward, district, district_id, city, city_id, country, country_id, map, phone, altphone, fax', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'place_id' => 'Place',
			'address' => 'Địa chỉ',
			'ward' => 'Phường',
			'district' => 'Khu vực khác',
			'district_id' => 'Khu vực',
			'city' => 'Thành phố khác',
			'city_id' => 'Thành phố',
			'country' => 'Quốc gia khác',
			'country_id' => 'Quốc gia',
			'map' => 'Map',
			'phone' => 'Điện thoại',
			'altphone' => 'Điện thoại khác',
			'fax' => 'Fax',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('place_id',$this->place_id,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('ward',$this->ward,true);
		$criteria->compare('district',$this->district,true);
		$criteria->compare('district_id',$this->district_id);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('city_id',$this->city_id);
		$criteria->compare('country',$this->country,true);
		$criteria->compare('country_id',$this->country_id);
		$criteria->compare('map',$this->map,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('altphone',$this->altphone,true);
		$criteria->compare('fax',$this->fax,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return PlaceLocations the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    
    public function getDistrict()
    {
        $model = Districts::model()->findAll();
        return CHtml::listData($model, 'id', 'name');
    }
    
    public function getCity()
    {
        $model = Cities::model()->findAll();
        return CHtml::listData($model, 'id', 'name');
    }
    
    public function getCountries()
    {
        $model = Countries::model()->findAll();
        return CHtml::listData($model, 'id', 'name');
    }
}
